import type { FollowType, ConsultOrderItem } from '@/types/consult'
import {
  followOrUnfollow,
  getPrescriptionPic,
  cancelOrder,
  deleteOrder
} from '@/services/consult'
import { sendMobileCode } from '@/services/user'
import {
  showImagePreview,
  showToast,
  showSuccessToast,
  type FormInstance
} from 'vant'
import { OrderType } from '@/enums'
import { getMedicalOrderDetail } from '@/services/order'
import type { OrderDetail } from '@/types/order'
import { ref, onMounted, type Ref, onUnmounted } from 'vue'
import type { CodeType } from '@/types/user'
// 逻辑封装 命名规范  useXxx  标识使用某公共

export const useFollow = (type: FollowType = 'doc') => {
  // 关注与取消关注功能
  const loading = ref(false)
  // {a, b}   传值的时候 {a, b , c} 也是可以的  类型兼容 只能多的给少的
  const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followOrUnfollow(item.id, type)
      item.likeFlag = item.likeFlag === 1 ? 0 : 1
    } finally {
      loading.value = false
    }
  }
  return {
    loading,
    follow
  }
}

/**
 * 预览处方
 */
export const useShowPrescriptionPic = () => {
  // 预览处方单
  const showPrescription = async (id: string) => {
    if (id) {
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return {
    showPrescription
  }
}

/**
 *  取消订单
 */
export const useCancelOrder = () => {
  // 取消订单
  const loading = ref(false)
  const cancelConsultOrder = (item: ConsultOrderItem) => {
    loading.value = true
    cancelOrder(item.id)
      .then(() => {
        item.status = OrderType.ConsultCancel
        item.statusValue = '已取消'
        showToast('取消成功')
        loading.value = false
      })
      .catch(() => {
        showToast('取消失败')
        loading.value = false
      })
    // .finally(() => {
    //   loading.value = false
    // })
  }
  return {
    loading,
    cancelConsultOrder
  }
}

/**
 * 删除订单
 *
 */

export const useDeleteOrder = (cb: () => void) => {
  const deleteLoading = ref(false)
  const deleteConsultOrder = (item: ConsultOrderItem) => {
    deleteLoading.value = true
    deleteOrder(item.id)
      .then(() => {
        // 变化的地方  详情页和列表页删除操作有区别
        cb && cb()
        showToast('删除成功')
        deleteLoading.value = false
      })
      .catch(() => {
        showToast('删除失败')
        deleteLoading.value = false
      })
  }
  return {
    deleteLoading,
    deleteConsultOrder
  }
}

/**
 * 获取订单详情接口数据
 */
export const useOrderDetail = (id: string) => {
  const loading = ref(false)
  const order = ref<OrderDetail>()
  onMounted(async () => {
    loading.value = true
    try {
      const res = await getMedicalOrderDetail(id)
      order.value = res.data
    } finally {
      loading.value = false
    }
  })
  return {
    order,
    loading
  }
}

/**
 * 发送验证码
 */
export const useSendMobileCode = (
  mobile: Ref<string>,
  type: CodeType = 'login'
) => {
  // 发送验证码
  const time = ref(0)
  const form = ref<FormInstance>()
  let timerId: number
  const send = async () => {
    // 已经倒计时的time值大于0 的时候,不应该发送验证码
    if (time.value > 0) return
    // 单独校验一下手机号
    await form.value?.validate('mobile')
    console.log('手机号校验成功了')
    await sendMobileCode(mobile.value, type)
    showSuccessToast('发送成功')
    // 倒计时
    time.value = 60
    //先清空倒计时防止内存泄漏
    clearInterval(timerId)
    timerId = window.setInterval(() => {
      time.value--
      if (time.value <= 0) window.clearInterval(timerId)
    }, 1000)
  }
  //关闭页面的时候需要清空定时器
  onUnmounted(() => {
    window.clearInterval(timerId)
  })
  return {
    form,
    time,
    send
  }
}
